package com.liqi.frame.comm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.liqi.frame.comm.dto.OperationDto;
import com.liqi.frame.comm.dao.PermissionDao;
import com.liqi.frame.comm.dao.RolePermissionDao;
import com.liqi.frame.comm.enums.PermissionTypeEnum;
import com.liqi.frame.comm.po.Permission;
import com.liqi.frame.comm.service.OperationService;
import com.liqi.frame.comm.service.PermissionService;
import com.liqi.frame.comm.token.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 操作
 * Created by liqi on 2017/12/20.
 * liqiwork@qq.com
 */
@Service("operationService")
public class OperationServiceImpl implements OperationService {

    @Override
    public List<OperationDto> getOperationList() {
        List<Permission> list =  permissionDao.getOperationList();
        OperationDto operation = null;
        List<OperationDto> operationList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            operation = new OperationDto();
            BeanUtil.copyProperties(list.get(i), operation);
            operation.setOperationKey(list.get(i).getPermissionKey());
            operation.setOperationName(list.get(i).getPermissionName());
            operationList.add(operation);
        }
        return operationList;
    }

    @Override
    public List<OperationDto> getOperationListByUserId(String userId) {
        List<Permission> list =  permissionDao.getOperationListByUserId(userId);
        OperationDto operation = null;
        List<OperationDto> operationList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            operation = new OperationDto();
            BeanUtil.copyProperties(list.get(i), operation);
            operation.setOperationKey(list.get(i).getPermissionKey());
            operation.setOperationName(list.get(i).getPermissionName());
            operationList.add(operation);
        }
        return operationList;
    }

    @Override
    public List<OperationDto> getOperationListByUsername(String username) {
        List<Permission> list =  permissionDao.getOperationListByUsername(username);
        OperationDto operation = null;
        List<OperationDto> operationList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            operation = new OperationDto();
            BeanUtil.copyProperties(list.get(i), operation);
            operation.setOperationKey(list.get(i).getPermissionKey());
            operation.setOperationName(list.get(i).getPermissionName());
            operationList.add(operation);
        }
        return operationList;
    }

    @Override
    public List<OperationDto> getOperationListByRoleId(String roleId) {
        List<Permission> list =  permissionDao.getOperationListByRoleId(roleId);
        OperationDto operation = null;
        List<OperationDto> operationList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            operation = new OperationDto();
            BeanUtil.copyProperties(list.get(i), operation);
            operation.setOperationKey(list.get(i).getPermissionKey());
            operation.setOperationName(list.get(i).getPermissionName());
            operationList.add(operation);
        }
        return operationList;
    }

    @Override
    public OperationDto getOperationById(String id) {
        Permission permission = permissionDao.getPermissionById(id);
        OperationDto operation = new OperationDto();
        BeanUtil.copyProperties(permission, operation);
        operation.setOperationKey(permission.getPermissionKey());
        operation.setOperationName(permission.getPermissionName());
        return operation;
    }

    @Override
    public List<OperationDto> getOperationListWithName(String operationName) {
        if (operationName!=null && !Objects.equals(operationName, "null")) {
            operationName = operationName.trim();
        }
        List<Permission> list = permissionDao.getPermissionListWithName(operationName, PermissionTypeEnum.OPERATION.getKey());
        OperationDto operation = null;
        List<OperationDto> operationList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            operation = new OperationDto();
            BeanUtil.copyProperties(list.get(i), operation);
            operation.setOperationKey(list.get(i).getPermissionKey());
            operation.setOperationName(list.get(i).getPermissionName());
            operationList.add(operation);
        }
        return operationList;
    }

    @Override
    public List<OperationDto> getOperationListWithNameByCurrUser(String operationName) {
        if (operationName!=null) {
            operationName = operationName.trim();
        }
        List<Permission> list = new ArrayList<>();
        if (TokenUtil.isAdministrator()) {
            list = permissionDao.getPermissionListWithName(operationName, PermissionTypeEnum.OPERATION.getKey());
        } else {
            list = permissionDao.getPermissionListWithNameByUserId(operationName, PermissionTypeEnum.OPERATION.getKey(), TokenUtil.getUserId());
        }
        OperationDto operation = null;
        List<OperationDto> operationList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            operation = new OperationDto();
            BeanUtil.copyProperties(list.get(i), operation);
            operation.setOperationKey(list.get(i).getPermissionKey());
            operation.setOperationName(list.get(i).getPermissionName());
            operationList.add(operation);
        }
        return operationList;
    }

    @Override
    public List<OperationDto> getOperationListByKey(String operationKey) {
        List<Permission> list = permissionDao.getPermissionsByKey(operationKey, PermissionTypeEnum.OPERATION.getKey());
        OperationDto operation = null;
        List<OperationDto> operationList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            operation = new OperationDto();
            BeanUtil.copyProperties(list.get(i), operation);
            operation.setOperationKey(list.get(i).getPermissionKey());
            operation.setOperationName(list.get(i).getPermissionName());
            operationList.add(operation);
        }
        return operationList;
    }

    @Override
    public int insertOperation(String id, String operationKey, String operationName, String comment, Float orderId) {
        return permissionService.insertPermission(id, operationKey, operationName, PermissionTypeEnum.OPERATION.getKey(), "", 10, comment, orderId, "", "");
    }

    @Override
    public int updateOperation(String id, String operationKey, String operationName, String comment, Float orderId) {
        return permissionService.updatePermission(id, operationKey, operationName, null, null, null, comment, orderId, "", "");
    }

    @Override
    public int deleteOperationById(String id) {
        int resultCount = 0;
        resultCount = permissionService.deletePermissionById(id);
        rolePermissionDao.deleteRoleAndPermissionsByPermissionId(id);
        return resultCount;
    }

    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionDao rolePermissionDao;
}
